﻿using System.Collections.Generic;
using KMF.Common;

namespace KMF.DAL
{
    public partial class PositionDb 
    {
        /// <summary>
        /// Lấy về danh sách tất cả các vị trí đặt Banner trên trang
        /// </summary>
        /// <returns></returns>
        public static List<ViewPositionDto> GetAllPosition()
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Position_GetAll");
            try
            {
                return dao.GetDTOList<ViewPositionDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Lấy về danh sách các bản ghi theo PositionKey
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static List<PositionDto> GetAllByKey(string key)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Position_GetAllByKey");
            try
            {
                return dao.GetDTOList<PositionDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Thêm mới vị trí
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int AddPosition(PositionDto model)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Position_AddPosition");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@PageId", model.PageId));
                command.Parameters.Add(dao.CreateParameter("@Name", model.Name));
                command.Parameters.Add(dao.CreateParameter("@PositionKey", model.PositionKey));
                command.Parameters.Add(dao.CreateParameter("@CreateDate", model.CreateDate));
                command.Parameters.Add(dao.CreateParameter("@Order", model.Order));
                return (int)command.ExecuteScalar();
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Xóa vị trí, có kiểm tra xem đã có Banner hay chưa
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int DeletePosition(int id)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Position_DeleteById");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@PositionId", id));
                return command.ExecuteNonQuery();
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}
